package com.dk.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dk.pojo.TRole;
import com.dk.service.RoleService;

@Controller
@RequestMapping("roleController")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	//��ת����ѯ��ɫҳ��
	@RequestMapping("list")
	public String list() {
		
		return "role/list";
	}
	
	//��ѯ���н�ɫ
	@RequestMapping("findRoleList")
	@ResponseBody
	public List<TRole> findRoleList(){
		
		List<TRole> roles = roleService.findRoleList();
		return roles;
	}
	
	//��ת�����ӽ�ɫҳ��
	@RequestMapping("toAdd")
	public String toAdd() {
		
		return "role/add";
	}
	
	//����
	@RequestMapping("addRole")
	@ResponseBody
	public String addRole(String roleName,@RequestParam("menuIds[]") Integer[] menuIds) {
		roleService.addRole(roleName,menuIds);
		return "1";
	}
	
	//ɾ��
	@RequestMapping("deleteInfoById/{roleId}")
	@ResponseBody
	public String deleteInfoById(@PathVariable("roleId") Integer roleId) {
		roleService.deleteInfoById(roleId);
		return "1";
	}
	
	//�޸�
	@RequestMapping("updateInfo/{roleId}")
	public String updateInfo(@PathVariable("roleId") Integer roleId,Model model) {
		TRole role = roleService.selectRoleById(roleId);
		model.addAttribute("role", role);
		return "role/update";
	}
	
	//��ɫ��ṹ���ɫ�޸�
	@RequestMapping("updateRole")
	@ResponseBody
	public boolean updateRole(String roleName,@RequestParam("menuIds[]") Integer[] menuIds, TRole role) {
		int i = roleService.updateRole(roleName,menuIds,role);
		return i > 0;
	}
}
